import { action, observable } from 'mobx';
import { listCategory, listCfg, saveCfg, deleteCfg } from '../service';
import { message } from 'antd';
import loading from '../../../components/Loading';
import  '../../../components/ChangeTheme';
class AppStore {

  @observable cfgList = {
    dataSource: [],
    total: 0,
    loading: false,
    pageSize: 10,
    pageNo: 1,
  };

  @observable categoryList = [];

  // 系统参数弹出框参数
  @observable cfgDialog = {
    loading: false,
    visible: false,
    editData: {},
  };

  /**
   * 析构函数
   */
  constructor() {
    this.initData();
  }

  /**
   * 初始化页面，加载数据
   * @returns {Promise<void>}
   */
  async initData() {
    this.categoryList = await listCategory();
    this.initListCfg();
  }

  async initListCfg() {
    let { pageNo, pageSize, categoryId, name } = this.cfgList;
    let data = await loading(listCfg, this.cfgList)(pageNo, pageSize, categoryId, name);
    this.cfgList = { ...this.cfgList, dataSource: data.rows, total: data.total };
  }

  @action
  showCfgDialog(params) {
    this.cfgDialog = { ...this.cfgDialog, visible: true, editData: params ? params : {} };
  }

  @action
  closeCfgDialog() {
    this.cfgDialog.visible = false;
  }

  @action
  async saveCfg(data) {
    let result = await loading(saveCfg, this.cfgDialog)(data);
    if (result.code == 0) {
      this.cfgDialog = { ...this.cfgDialog, visible: false };
      this.initListCfg();
      message.info('保存数据字典成功');
    } else {
      message.warning(result.msg);
    }

  }

  @action
  listCfgHandler(data) {
    this.cfgList = { ...this.cfgList, pageNo: 1, categoryId: data.categoryId, name: data.name };
    this.initListCfg();
  }

  @action
  async paginationHandler(pageNo, pageSize) {
    this.cfgList = { ...this.cfgList, pageNo: pageNo, pageSize: pageSize };
    this.initListCfg();
  }

  @action
  async deleteCfg(record) {
    await loading(deleteCfg, this.cfgList)(record);
    await this.initListCfg();
  }
}

export default AppStore;
